<!doctype html>



  


<html class="theme-next mist use-motion" lang="en">
<head>
  <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="postgreSQL," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="PostgreSQL for Beginners">
<meta property="og:type" content="article">
<meta property="og:title" content="PostgreSQL_for_beginners">
<meta property="og:url" content="https://annashuo.github.io/2017/08/25/postgreSQL-for-beginners/index.html">
<meta property="og:site_name" content="Anna's Blog">
<meta property="og:description" content="PostgreSQL for Beginners">
<meta property="og:image" content="https://github.com/Annashuo/hello-world/blob/master/numerial_data_type.png?raw=true">
<meta property="og:image" content="https://github.com/Annashuo/hello-world/blob/master/postgres_operator.png?raw=true">
<meta property="og:image" content="https://github.com/Annashuo/hello-world/blob/master/INNER_JOIN.png?raw=true">
<meta property="og:image" content="https://github.com/Annashuo/hello-world/blob/master/LEFT_JOIN.png?raw=true">
<meta property="og:image" content="https://github.com/Annashuo/hello-world/blob/master/OUTER_JOIN.png?raw=true">
<meta property="og:updated_time" content="2017-09-14T19:36:28.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="PostgreSQL_for_beginners">
<meta name="twitter:description" content="PostgreSQL for Beginners">
<meta name="twitter:image" content="https://github.com/Annashuo/hello-world/blob/master/numerial_data_type.png?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://annashuo.github.io/2017/08/25/postgreSQL-for-beginners/"/>





  <title> PostgreSQL_for_beginners | Anna's Blog </title><!-- hexo-inject:begin --><!-- hexo-inject:end -->
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="en">

  
<!-- hexo-inject:begin --><!-- hexo-inject:end --><script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '',
      xfbml      : true,
      version    : 'v2.6'
    });
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>



<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-105321506-1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?6f5ab2ef1e021070500f7228c830f3c6";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">Anna's Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
    
      <p class="site-subtitle">My heart is in the work...</p>
    
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="https://annashuo.github.io/2017/08/25/postgreSQL-for-beginners/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Anna">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/images/avatar.gif">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Anna's Blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Anna's Blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                PostgreSQL_for_beginners
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-08-25T13:57:15-04:00">
                2017-08-25
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/STUDY/" itemprop="url" rel="index">
                    <span itemprop="name">STUDY</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/08/25/postgreSQL-for-beginners/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count fb-comments-count" data-href="https://annashuo.github.io/2017/08/25/postgreSQL-for-beginners/" itemprop="commentCount">0</span> comments
                </a>
              </span>
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>PostgreSQL for Beginners<br><a id="more"></a></p>
<h4 id="Server-Service"><a href="#Server-Service" class="headerlink" title="Server Service"></a>Server Service</h4><p>You can install multiple servers in a physical server using different ports and also have different locations to store data. </p>
<h4 id="Database"><a href="#Database" class="headerlink" title="Database"></a>Database</h4><p>A database is container of all the objects such as tables, functions, sequences and trigger.</p>
<ul>
<li>Table</li>
<li><p>Schema: </p>
<ul>
<li><p>A logical container of the tables and other database objects.</p>
</li>
<li><p>It is possible to assign permission to schemas, so you can restrict who can access the objects.</p>
</li>
<li><p>You can have multiple schemas in a database.</p>
</li>
</ul>
</li>
<li><p>Tablespace: </p>
<ul>
<li><p>Storage location where data is stored.</p>
</li>
<li><p>By default, PostgreSQL provides two tablespaces: <em>pg_default</em> and <em>pg_global</em></p>
<p>  <em>pg_default</em> is used for storing user data.</p>
<p>  <em>pg_global</em> is used for storing system data.</p>
</li>
</ul>
</li>
<li><p>Views</p>
<ul>
<li><p>Virtual tables that are used to basically simplify complex queries.</p>
</li>
<li><p>Used to apply security for a set of records. You can use view to hide information or protect certain parts of a table from others to see, so you can create a view from a table.</p>
</li>
</ul>
</li>
<li><p>Functions</p>
<ul>
<li><p>Block of reuseable SQL Code.</p>
</li>
<li><p>Returns acalar value of a list of records.</p>
</li>
<li>Return composite objects.</li>
</ul>
</li>
<li><p>Cast </p>
<ul>
<li>Convert one data type into another data type. </li>
<li>It is used with functions to perform conversion.</li>
<li>You can create custom casts to overide the default.</li>
</ul>
</li>
<li><p>Operator</p>
<ul>
<li>A symbolic function</li>
<li>You can define your own custom operators</li>
</ul>
</li>
<li><p>Sequences</p>
<ul>
<li>Used to manage auto-increment columns</li>
</ul>
</li>
<li><p>Extension</p>
<ul>
<li>Used to wrap other objects such as casts, indexes, functions and others into a single unit</li>
</ul>
</li>
</ul>
<h4 id="PostreSQL-Fundamentals"><a href="#PostreSQL-Fundamentals" class="headerlink" title="PostreSQL Fundamentals"></a>PostreSQL Fundamentals</h4><h5 id="DATA-Types"><a href="#DATA-Types" class="headerlink" title="DATA Types"></a>DATA Types</h5><ul>
<li>Boolean: True, False, NULL</li>
<li>Character: Char(n), Varchar(n), text</li>
<li>Number: small integer(smallint), integer(int), serial(autoincrementing integer), float(n), real or float8, numeric or numeric(p,s)</li>
</ul>
<p><img src="https://github.com/Annashuo/hello-world/blob/master/numerial_data_type.png?raw=true" alt=""></p>
<ul>
<li><p>Temporal: </p>
<ul>
<li><strong>date</strong> store date values only</li>
<li><strong>time</strong> store time of the day values</li>
<li><strong>timestamp</strong> store data and time</li>
<li><strong>interval</strong> store periods of time</li>
<li><strong>timestamptz</strong> store both timestamp and time zone data</li>
</ul>
</li>
<li><p>Array</p>
</li>
<li><p>Special types</p>
<ul>
<li><strong>box</strong>: a rectangular box</li>
<li><strong>line</strong>: a set of points</li>
<li><strong>point</strong>: a geometric pair of numbers</li>
<li><strong>lseg</strong>: a line segment</li>
<li><strong>polygon</strong>: a closed geometric</li>
<li><strong>inet</strong>: an IP4 address</li>
<li><strong>macaddr</strong>: a MAC address</li>
</ul>
</li>
</ul>
<h5 id="UNIQUE-constraints"><a href="#UNIQUE-constraints" class="headerlink" title="UNIQUE constraints"></a>UNIQUE constraints</h5><p>Ensures values in a column or group of columns are unique in a table.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">CREATE TABLE person(</div><div class="line">	id serial PRIMARY KEY,</div><div class="line">	first_name VARCHAR(50),</div><div class="line">	last_name VARCHAR(50),</div><div class="line">	email VARCHAR(50) UNIQUE</div><div class="line">);</div></pre></td></tr></table></figure>
<h5 id="WHERE"><a href="#WHERE" class="headerlink" title="WHERE"></a>WHERE</h5><p>Filtering rows returned from SELECT query<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">SELECT column1, column2</div><div class="line">FROM table_name</div><div class="line">WHERE conditions;</div></pre></td></tr></table></figure></p>
<h5 id="SELECT"><a href="#SELECT" class="headerlink" title="SELECT"></a>SELECT</h5><p>Query data from all columns</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">SELECT *</div><div class="line">FROM table_name;</div><div class="line"></div><div class="line">SELECT column1, column2</div><div class="line">FROM table_name;</div></pre></td></tr></table></figure>
<h5 id="DISTINCT"><a href="#DISTINCT" class="headerlink" title="DISTINCT"></a>DISTINCT</h5><p>Removing duplicate records</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">SELECT DISTINCT column1, column2</div><div class="line">FROM table_name</div><div class="line">ORDER BY</div><div class="line">column1 ASC,</div><div class="line">column2 DESC;</div></pre></td></tr></table></figure>
<h5 id="ORDER-BY"><a href="#ORDER-BY" class="headerlink" title="ORDER BY"></a>ORDER BY</h5><p>Sort data returned by SELECT statement, default is ASC.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">SELECT DISTINCT column1, column2</div><div class="line">FROM table_name</div><div class="line">ORDER BY</div><div class="line">column1 ASC,</div><div class="line">column2 DESC;</div></pre></td></tr></table></figure>
<h5 id="GROUP-BY"><a href="#GROUP-BY" class="headerlink" title="GROUP BY"></a>GROUP BY</h5><p>Used to divide rows return ed from a SELECT statement into groups.</p>
<p>You can apply aggregate function e.g SUM and COUNT</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">SELECT column1, aggregate_function(column2)</div><div class="line">FROM table1</div><div class="line">GROUP BY column1;</div></pre></td></tr></table></figure>
<h5 id="AGGREGATE-FUNCTION"><a href="#AGGREGATE-FUNCTION" class="headerlink" title="AGGREGATE FUNCTION"></a>AGGREGATE FUNCTION</h5><p>Used to perform calculations on data</p>
<p>They return a single value calculated from values in a column</p>
<div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">FUNCTION</th>
<th style="text-align:center">DESCRIPTION </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">AVG()</td>
<td style="text-align:center">Return average value</td>
</tr>
<tr>
<td style="text-align:center">COUNT()</td>
<td style="text-align:center">Return number of rows</td>
</tr>
<tr>
<td style="text-align:center">MAX()</td>
<td style="text-align:center">Return the largest value</td>
</tr>
<tr>
<td style="text-align:center">MIN()</td>
<td style="text-align:center">Return smallest value</td>
</tr>
<tr>
<td style="text-align:center">SUM()</td>
<td style="text-align:center">Return the sum</td>
</tr>
<tr>
<td style="text-align:center">FIRST() and LAST()</td>
<td style="text-align:center">Return first and last value</td>
</tr>
</tbody>
</table>
</div>
<h5 id="HAVING"><a href="#HAVING" class="headerlink" title="HAVING"></a>HAVING</h5><p>Used to in conjunction with GROUP BY clause</p>
<p>Used filter group rows that do not satisfy a specified condition.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">SELECT column1, aggregate_function(column2)</div><div class="line">FROM table_name</div><div class="line">GROUP BY column1</div><div class="line">HAVING condition;</div></pre></td></tr></table></figure>
<h4 id="TRUNCATE-and-DROP"><a href="#TRUNCATE-and-DROP" class="headerlink" title="TRUNCATE and DROP"></a>TRUNCATE and DROP</h4><p>TRUNCATE removes all data from a table</p>
<p>DROP delete the table from the database</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">TRUNCATE TABLE table_name;</div><div class="line">DROP TABLE table_name;</div></pre></td></tr></table></figure>
<h4 id="Postgres-CRUD-Operations"><a href="#Postgres-CRUD-Operations" class="headerlink" title="Postgres CRUD Operations"></a>Postgres CRUD Operations</h4><h5 id="Create-database-and-table"><a href="#Create-database-and-table" class="headerlink" title="Create database and table"></a>Create database and table</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">CREATE TABLE table_name(</div><div class="line">	column1_name datatype column_constraint,</div><div class="line">	column2_name datatype column_constraint,</div><div class="line">);</div></pre></td></tr></table></figure>
<h5 id="Read-table"><a href="#Read-table" class="headerlink" title="Read table"></a>Read table</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">SELECT columns</div><div class="line">FROM table_name;</div></pre></td></tr></table></figure>
<h5 id="Subquery"><a href="#Subquery" class="headerlink" title="Subquery"></a>Subquery</h5><p>Allows you to construct complex queries.</p>
<p>Query nested inside another query such as SELECT.</p>
<p>To construct a subquery the second query is placed in brackets.</p>
<p>The WHERE clause is used as expression in the subquery.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">SELECT &quot;column_name1&quot;</div><div class="line">FROM &quot;table_name1&quot;</div><div class="line">WHERE &quot;column_name2&quot;[Comparison Operator]</div><div class="line">(SELECT &quot;column_name3&quot;</div><div class="line">FROM &quot;table_name2&quot;</div><div class="line">WHERE &quot;condition&quot;);</div></pre></td></tr></table></figure>
<h5 id="UPDATE"><a href="#UPDATE" class="headerlink" title="UPDATE"></a>UPDATE</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">UPDATE table_name</div><div class="line">SET column1=value1,</div><div class="line">    column2=value2</div><div class="line">WHERE condition;</div></pre></td></tr></table></figure>
<h5 id="DELETE"><a href="#DELETE" class="headerlink" title="DELETE"></a>DELETE</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">DELECT FROM table_name</div><div class="line">WHERE condition;</div></pre></td></tr></table></figure>
<h5 id="INSERT"><a href="#INSERT" class="headerlink" title="INSERT"></a>INSERT</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">INSERT INTO table_name(column1, column2)</div><div class="line">VALUES(value1, value2);</div></pre></td></tr></table></figure>
<h4 id="OPERATOR"><a href="#OPERATOR" class="headerlink" title="OPERATOR"></a>OPERATOR</h4><h5 id="Comparison"><a href="#Comparison" class="headerlink" title="Comparison"></a>Comparison</h5><p><img src="https://github.com/Annashuo/hello-world/blob/master/postgres_operator.png?raw=true" alt=""></p>
<h5 id="BETWEEN-OR-NOT-BETWEEN"><a href="#BETWEEN-OR-NOT-BETWEEN" class="headerlink" title="BETWEEN OR NOT BETWEEN"></a>BETWEEN OR NOT BETWEEN</h5><p>Values can be numbers, text or dates</p>
<p>Included in the WHERE clause</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">SELECT column_names</div><div class="line">FROM table_name</div><div class="line">WHERE column_name (NOT) BETWEEN value1 AND value2;</div></pre></td></tr></table></figure>
<h5 id="NOT"><a href="#NOT" class="headerlink" title="NOT"></a>NOT</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">SELECT coloumn_names</div><div class="line">FROM table_name</div><div class="line">WHERE NOT condition</div></pre></td></tr></table></figure>
<h5 id="LIKE"><a href="#LIKE" class="headerlink" title="LIKE"></a>LIKE</h5><p>Match patten in a column.</p>
<p>% wildcard allows you to match string of any length characters including zero.</p>
<p>_ wildcard allows you to match a single character</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">SELECT column1, column2</div><div class="line">FROM table_name</div><div class="line">WHERE column LIKE patten;</div></pre></td></tr></table></figure>
<h5 id="OR-amp-AND"><a href="#OR-amp-AND" class="headerlink" title="OR &amp; AND"></a>OR &amp; AND</h5><p>AND operator and OP operator can be used in SELECT, INSERT, UPDATE, DELECT.</p>
<p>Use parenthesis () when combining conditions.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">SELECT column</div><div class="line">FROM table_name</div><div class="line">WHERE condition AND/OR condition</div></pre></td></tr></table></figure>
<h5 id="LIMIT"><a href="#LIMIT" class="headerlink" title="LIMIT"></a>LIMIT</h5><p>Gets a subset of row returned by a query</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">SELECT * </div><div class="line">FROM table_name</div><div class="line">LIMIT n;</div></pre></td></tr></table></figure>
<h5 id="IN"><a href="#IN" class="headerlink" title="IN"></a>IN</h5><p>In a <strong>list</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">SELECT column_name</div><div class="line">FROM table_name</div><div class="line">WHERE column_name IN (value1, value2)</div></pre></td></tr></table></figure>
<h5 id="UNION-amp-UNION-ALL"><a href="#UNION-amp-UNION-ALL" class="headerlink" title="UNION &amp; UNION ALL"></a>UNION &amp; UNION ALL</h5><p>Used to combine result sets of multiple queries into a single result.</p>
<p>Without <strong>ALL</strong>, removes all duplicate rows. With <strong>ALL</strong>, does not remove duplicate rows.</p>
<p>Both queries must return same number of columns.</p>
<p>The corresponding columns in the queries must have compatible data type.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">SELECT column1, column2</div><div class="line">FROM table1</div><div class="line">UNION</div><div class="line">SELECT column1, column2</div><div class="line">FROM table2;</div></pre></td></tr></table></figure>
<h5 id="INTERSECT"><a href="#INTERSECT" class="headerlink" title="INTERSECT"></a>INTERSECT</h5><p>Used to combine result set of two or more SELECT statements into a single result.</p>
<p>The INTERSECT operator returns all rows in both result sets.</p>
<p>The number of columns that appear in the SELECT statement must be the same.</p>
<p>The data types of the columns must be compatible.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">SELECT column1, column2</div><div class="line">FROM table1</div><div class="line">INTERSECT</div><div class="line">SELECT column1, column2</div><div class="line">FROM table2;</div></pre></td></tr></table></figure>
<h5 id="EXCEPT"><a href="#EXCEPT" class="headerlink" title="EXCEPT"></a>EXCEPT</h5><p>Retuen rows by comparing the result sets of two or more queries.</p>
<p>Return rows in the firdt query not present in output of the second query.</p>
<p>Returns distinct rows from the first(left) query not in out of the second(right) query.</p>
<p>The number of colums and their orders must be the same in both queries.</p>
<p>The data types of the respective columns must be compatible.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">SELECT column1, column2</div><div class="line">FROM table1</div><div class="line">WHERE conditionA</div><div class="line">EXCEPT</div><div class="line">SELECT column1, column2</div><div class="line">FROM table2</div><div class="line">WHERE conditionB;</div></pre></td></tr></table></figure>
<h4 id="JOINS"><a href="#JOINS" class="headerlink" title="JOINS"></a>JOINS</h4><p>Used to retrieve data from multiple tables.</p>
<p>Joins is performed when two or more tables are joined in a SQL statement.</p>
<p>Tables participating in the join should have related columns between them.</p>
<p>Types of Joins</p>
<ul>
<li>INNER JOIN</li>
<li>LEFT JOIN</li>
<li>OUTER JOIN</li>
<li>CROSS JOIN: produce cartesian product of rows from combind joined table. Tables can have no matching columns</li>
<li>NATURAL JOIN: create an implicit join based on matching column names in the tables. It can be INNER JOIN, LEFT JOIN, RIGHT JOIN. Default is INNER JOIN.</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">SELECT column_name(s)</div><div class="line">FROM tableA</div><div class="line">(INNER/LEFT/OURTER) JOIN tableB ON tableA.column = tableB.column;</div><div class="line"></div><div class="line">SELECT *</div><div class="line">FROM table1 </div><div class="line">(CROSS/NATURAL) JOIN table2;</div></pre></td></tr></table></figure>
<p>INNER JOIN:<br><img src="https://github.com/Annashuo/hello-world/blob/master/INNER_JOIN.png?raw=true" alt=""></p>
<p>LEFT JOIN:<br><img src="https://github.com/Annashuo/hello-world/blob/master/LEFT_JOIN.png?raw=true" alt=""></p>
<p>OURTER JOIN:<br><img src="https://github.com/Annashuo/hello-world/blob/master/OUTER_JOIN.png?raw=true" alt=""></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/postgreSQL/" rel="tag"># postgreSQL</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/08/24/llvm和jit/" rel="next" title="llvm和jit">
                <i class="fa fa-chevron-left"></i> llvm和jit
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/14/database-system-review01/" rel="prev" title="Database System Review 01">
                Database System Review 01 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="fb-comments"
           data-href="https://annashuo.github.io/2017/08/25/postgreSQL-for-beginners/"
           data-numposts="10"
           data-width="100%"
           data-colorscheme="light">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="Anna" />
          <p class="site-author-name" itemprop="name">Anna</p>
          <p class="site-description motion-element" itemprop="description">My heart is in the work...</p>
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">43</span>
                <span class="site-state-item-name">posts</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">12</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/Annashuo/Annashuo.github.io" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/u/1861044584?is_all=1" target="_blank" title="Weibo">
                  
                    <i class="fa fa-fw fa-weibo"></i>
                  
                  Weibo
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#Server-Service"><span class="nav-number">1.</span> <span class="nav-text">Server Service</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Database"><span class="nav-number">2.</span> <span class="nav-text">Database</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#PostreSQL-Fundamentals"><span class="nav-number">3.</span> <span class="nav-text">PostreSQL Fundamentals</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#DATA-Types"><span class="nav-number">3.1.</span> <span class="nav-text">DATA Types</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#UNIQUE-constraints"><span class="nav-number">3.2.</span> <span class="nav-text">UNIQUE constraints</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#WHERE"><span class="nav-number">3.3.</span> <span class="nav-text">WHERE</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#SELECT"><span class="nav-number">3.4.</span> <span class="nav-text">SELECT</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#DISTINCT"><span class="nav-number">3.5.</span> <span class="nav-text">DISTINCT</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#ORDER-BY"><span class="nav-number">3.6.</span> <span class="nav-text">ORDER BY</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#GROUP-BY"><span class="nav-number">3.7.</span> <span class="nav-text">GROUP BY</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#AGGREGATE-FUNCTION"><span class="nav-number">3.8.</span> <span class="nav-text">AGGREGATE FUNCTION</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#HAVING"><span class="nav-number">3.9.</span> <span class="nav-text">HAVING</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#TRUNCATE-and-DROP"><span class="nav-number">4.</span> <span class="nav-text">TRUNCATE and DROP</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Postgres-CRUD-Operations"><span class="nav-number">5.</span> <span class="nav-text">Postgres CRUD Operations</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#Create-database-and-table"><span class="nav-number">5.1.</span> <span class="nav-text">Create database and table</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Read-table"><span class="nav-number">5.2.</span> <span class="nav-text">Read table</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Subquery"><span class="nav-number">5.3.</span> <span class="nav-text">Subquery</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#UPDATE"><span class="nav-number">5.4.</span> <span class="nav-text">UPDATE</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#DELETE"><span class="nav-number">5.5.</span> <span class="nav-text">DELETE</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#INSERT"><span class="nav-number">5.6.</span> <span class="nav-text">INSERT</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#OPERATOR"><span class="nav-number">6.</span> <span class="nav-text">OPERATOR</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#Comparison"><span class="nav-number">6.1.</span> <span class="nav-text">Comparison</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#BETWEEN-OR-NOT-BETWEEN"><span class="nav-number">6.2.</span> <span class="nav-text">BETWEEN OR NOT BETWEEN</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#NOT"><span class="nav-number">6.3.</span> <span class="nav-text">NOT</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#LIKE"><span class="nav-number">6.4.</span> <span class="nav-text">LIKE</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#OR-amp-AND"><span class="nav-number">6.5.</span> <span class="nav-text">OR & AND</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#LIMIT"><span class="nav-number">6.6.</span> <span class="nav-text">LIMIT</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#IN"><span class="nav-number">6.7.</span> <span class="nav-text">IN</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#UNION-amp-UNION-ALL"><span class="nav-number">6.8.</span> <span class="nav-text">UNION & UNION ALL</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#INTERSECT"><span class="nav-number">6.9.</span> <span class="nav-text">INTERSECT</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#EXCEPT"><span class="nav-number">6.10.</span> <span class="nav-text">EXCEPT</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#JOINS"><span class="nav-number">7.</span> <span class="nav-text">JOINS</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Anna</span>
</div>


<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  

  
      <!-- UY BEGIN -->
      <script type="text/javascript" src="http://v2.uyan.cc/code/uyan.js?uid="></script>
      <!-- UY END --><!-- hexo-inject:begin --><!-- Begin: Injected MathJax -->
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({"tex2jax":{"inlineMath":[["$","$"],["\\(","\\)"]],"skipTags":["script","noscript","style","textarea","pre","code"],"processEscapes":true},"TeX":{"equationNumbers":{"autoNumber":"AMS"}}});
</script>

<script type="text/x-mathjax-config">
  MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
      all[i].SourceElement().parentNode.className += ' has-jax';
    }
  });
</script>

<script type="text/javascript" src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- End: Injected MathJax -->
<!-- hexo-inject:end -->
  




  
  

  
  


  

  

  


</body>
</html>
